package com.sskj.dgzs.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sskj.core.util.JsonUtil;
import com.sskj.dgzs.domain.DengGuangZhouShouQueryParam;
import com.sskj.dgzs.domain.entity.DengGuangZhuShouEntity;
import com.sskj.dgzs.repository.DengGuangZhuShouRepository;
import com.sskj.sys.domain.entity.MediaEntity;
import com.sskj.sys.repository.MediaRepository;
import com.sskj.utils.StringUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/wechat/dgzs")
public class DengGuangZhouShouServiceImpl {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	DengGuangZhuShouRepository dengGuangZhuShouRepository;

	@Autowired
	MediaRepository mediaRepository;

	@Value("${sskj.conf.mediaUploadPath}")
	private String mediaUploadPath;

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "**/addSuggession", method = { RequestMethod.POST })
	public void add(DengGuangZhuShouEntity dengGuangZhuShouEntity) {

		dengGuangZhuShouRepository.save(dengGuangZhuShouEntity);

		if (!dengGuangZhuShouEntity.getPicList().isEmpty()) {
			mediaRepository.updateMedia(dengGuangZhuShouEntity.getId(), dengGuangZhuShouEntity.getPicList());
		}

		mediaRepository.delMedia(dengGuangZhuShouEntity.getId(), dengGuangZhuShouEntity.getPicList().isEmpty()
				? StringUtil.toList("XXXXX", ",") : dengGuangZhuShouEntity.getPicList());

		logger.info(JsonUtil.json(dengGuangZhuShouEntity));
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "**/page", method = { RequestMethod.GET })
	@ApiImplicitParams({ @ApiImplicitParam(name = "text", value = "关键字", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "creatorId", value = "创建者ID", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "suggesttype", value = "类型", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "pageNumber", value = "页号", dataType = "int"),
			@ApiImplicitParam(name = "pageSize", value = "页的大小", dataType = "int") })
	public Page<DengGuangZhuShouEntity> loadSuggestionByCId(
			@ApiIgnore DengGuangZhouShouQueryParam dengGuangZhouShouQueryParam, Pageable pageable,
			HttpServletRequest request) {

		Page<DengGuangZhuShouEntity> page = dengGuangZhuShouRepository.findPage(dengGuangZhouShouQueryParam, pageable);

		List<DengGuangZhuShouEntity> list = page.getContent();
		if (list != null) {
			for (DengGuangZhuShouEntity dg : list) {
				List<MediaEntity> mes = mediaRepository.queryPic(dg.getId());
				for (MediaEntity me : mes) {
					dg.getPicList().add(me.getId());
					dg.getUrlList().add(request.getScheme() + "://" + request.getServerName() + ":"
							+ request.getServerPort() + "/" + me.getHttpurl());
				}
			}
		}
		return page;
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "**/loadLastSuggestionByType", method = { RequestMethod.GET })
	public Map<String, Object> loadLastSuggestionOneByType(HttpServletRequest request) {

		Map<String, Object> result = new HashMap<String, Object>();

		DengGuangZhuShouEntity ld = dengGuangZhuShouRepository.queryLastSuggsionByType(1);
		DengGuangZhuShouEntity jgd = dengGuangZhuShouRepository.queryLastSuggsionByType(2);
		DengGuangZhuShouEntity yllh = dengGuangZhuShouRepository.queryLastSuggsionByType(3);
		DengGuangZhuShouEntity aqsg = dengGuangZhuShouRepository.queryLastSuggsionByType(4);

		if (null != ld) {
			getPicList(ld, request);
		}
		if (null != jgd) {
			getPicList(jgd, request);
		}
		if (null != yllh) {
			getPicList(yllh, request);
		}
		if (null != aqsg) {
			getPicList(aqsg, request);
		}

		result.put("ld", ld);
		result.put("jgd", jgd);
		result.put("yllh", yllh);
		result.put("aqsg", aqsg);

		logger.info(JsonUtil.json(result));

		return result;
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "**/fallBackSuggestion", method = { RequestMethod.POST })
	public void fallBackSuggestion(DengGuangZhouShouQueryParam dengGuangZhouShouQueryParam) {
		dengGuangZhuShouRepository.fallBackSuggsion(2, dengGuangZhouShouQueryParam.getId());

	}

	public DengGuangZhuShouEntity getPicList(DengGuangZhuShouEntity o, HttpServletRequest request) {
		List<MediaEntity> mes = mediaRepository.queryPic(o.getId());
		for (MediaEntity me : mes) {
			o.getPicList().add(me.getId());
			o.getUrlList().add(request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
					+ "/" + me.getHttpurl());
		}
		return o;
	}

}
